// Launcher.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Launcher.h"

#ifdef WIN32
#include <Windows.h>
#endif

using namespace Nezha;

PMap<_string, MiniFrameworkCreator*> MiniFramework::msCreators;
MiniFramework* MiniFramework::msMiniFramework = NULL;

void MiniFramework::SetFramework(const char* name)
{
	PMap<_string, MiniFrameworkCreator*>::iterator it = msCreators.find(name);
	
	if(it != msCreators.end())
	{
		msMiniFramework = it->second->create();
	}
}

void MiniFramework::RegisterFrameworkCreator(const char* name, MiniFrameworkCreator* c)
{
	msCreators[name] = c;
}

#ifdef WIN32

int _tmain(int argc, _TCHAR* argv[])
{

	EngineConfig cfg;

	if(argc > 1)
	{
		MiniFramework::SetFramework(argv[1]);
	}
	else
	{
#ifdef WIN32
		char ret[1024] = {0};
		//GetCurrentDirectory(1024, ret);
		//FILE* f = fopen("../Test/Platform/Test.cfg", "rb");
		LONG retSize = ::GetPrivateProfileString("Framework", "Name", "Basic", ret, 1024, "../Test/Platform/Test.cfg");
		if(retSize > 0)
		{
			MiniFramework::SetFramework(ret);
		}
		else
		{
			//MiniFramework::SetFramework("Basic");
		}
#else
		// for mac, replace this
		MiniFramework::SetFramework("Basic");
#endif
	}


	if(!MiniFramework::msMiniFramework)
	{
		printf("Failed to initialize framework!");
		return 0;
	}
	
	MiniFramework::msMiniFramework->initialSetConfig(&cfg);

	NezhaEngine* engine = NZ_New NezhaEngine();

	PArray<SubsystemAdaptor*> subsys;

	if(engine->initialize(cfg, subsys))
	{
		if(MiniFramework::msMiniFramework->initialize())
		{
			while(MiniFramework::msMiniFramework->externalUpdate() && engine->update());
		}
		else
		{

		}

		MiniFramework::msMiniFramework->uninitialize();

		engine->uninitialize();
	}
	else
	{

	}

	return 0;
}
#endif // 

